package com.lm.minio.controller;

import io.minio.ComposeObjectArgs;
import io.minio.ComposeSource;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    private MinioClient minioClient;

    @PostMapping("/chunk")
    public String uploadChunk(@RequestParam("file") MultipartFile file,
                              @RequestParam("chunkIndex") int chunkIndex,
                              @RequestParam("chunkTotal") int chunkTotal,
                              @RequestParam("identifier") String identifier) {
        try {
            String bucketName = "mybucket";
            String objectName = identifier + "/" + chunkIndex;
            InputStream inputStream = file.getInputStream();
            minioClient.putObject(
                    PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
                                    inputStream, file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build());
            return "Chunk " + chunkIndex + " uploaded successfully.";
        } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }

    @PostMapping("/merge")
    public String mergeChunks(@RequestParam("identifier") String identifier,
                              @RequestParam("chunkTotal") int chunkTotal) throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        try {
            String bucketName = "mybucket";
            List<ComposeSource> sources = new ArrayList<>();
            for (int i = 0; i < chunkTotal; i++) {
                sources.add(ComposeSource.builder()
                        .bucket(bucketName)
                        .object(identifier + "/" + i)
                        .build());
            }

            minioClient.composeObject(
                    ComposeObjectArgs.builder()
                            .bucket(bucketName)
                            .object(identifier)
                            .sources(sources)
                            .build());

            return "File merged successfully.";
        } catch (MinioException e) {
            e.printStackTrace();
            return "Error: " + e.getMessage();
        }
    }
}
